/*
	Copyright 2009 Omer Saatcioglu
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

		http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.

	$Author$
	$Revision$
	$LastChangedDate$
   
 */

package com.saatcioglu.android.guessthenumber;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class GfxMain extends Activity implements OnClickListener {

	static final private int MENU_BUTTON_NUMBER = 7;

	Button[] arrMenuButtons = new Button[MENU_BUTTON_NUMBER];

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		CtrlMaintanence ctrlMaintanence = new CtrlMaintanence();

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		this.setTitle(getString(R.string.app_name) + " v"
				+ getString(R.string.app_version));

		preparePreference();
		ctrlMaintanence.updatePreferences();
		setDisplayElements();
	}

	@Override
	public void onClick(View v) {
		int idButton = ((Button) v).getId();
		switch (idButton) {
		case R.id.menu_customshot:
			startActivityForResult(new Intent(this, GfxGuesstheNumber.class),
					idButton);
			break;
		case R.id.menu_customsettings:
			startActivityForResult(new Intent(this, GfxPreferences.class),
					idButton);
			break;
		case R.id.menu_options:
			startActivityForResult(new Intent(this, GfxOptions.class), idButton);
			break;
		case R.id.menu_help:
			showDialog(R.id.display_help);
			break;
		case R.id.menu_about:
			showDialog(R.id.display_about);
			break;
		case R.id.menu_exit:
			finish();
		case R.id.menu_user:
			showDialog(R.id.display_nameedit);
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case R.id.menu_customsettings:
			// TODO why resultCode does not send anything??
			// if(resultCode == Activity.RESULT_OK)
			if (SMBGuesstheNumber.isPreferencesChanged) {
				SMBGuesstheNumber.isGameStarted = false;
				(new CtrlMaintanence()).savetheGame();
			}
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case R.id.display_chechforupdate:
			return new AlertDialog.Builder(GfxMain.this).setIcon(
					R.drawable.droid).setTitle(R.string.dialog_update_title)
					.setMessage(R.string.dialog_update_text).setPositiveButton(
							R.string.OK, new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									Uri marketUri = Uri
											.parse(getString(R.string.url_update_address));
									Intent marketIntent = new Intent(
											Intent.ACTION_VIEW, marketUri);
									startActivity(marketIntent);
								}
							}).setNegativeButton(R.string.cancel,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
								}
							}).create();

		case R.id.display_help:
			return new AlertDialog.Builder(GfxMain.this).setIcon(
					R.drawable.droid).setTitle(R.string.dialog_help_title)
					.setMessage(R.string.dialog_help_text).setPositiveButton(
							R.string.OK, new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
								}
							}).create();
		case R.id.display_about:
			return new AlertDialog.Builder(GfxMain.this).setIcon(
					R.drawable.droid).setTitle(R.string.dialog_about_title)
					.setMessage(R.string.dialog_about_text).setPositiveButton(
							R.string.OK, new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
								}
							}).create();
		case R.id.display_nameedit:
			LayoutInflater factory = LayoutInflater.from(this);
			final View textEntryView = factory.inflate(
					R.layout.dialog_nameedit, null);
			return new AlertDialog.Builder(GfxMain.this).setIcon(
					R.drawable.droid).setTitle(R.string.dialog_text_entry)
					.setView(textEntryView).setPositiveButton(
							R.string.EnterName,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									(new CtrlMaintanence())
											.updatePlayerName(((TextView) textEntryView
													.findViewById(R.id.username_edit))
													.getText().toString());
									arrMenuButtons[0]
											.setText(SMBGuesstheNumber.strPlayerName);
								}
							}).setNegativeButton(R.string.cancel,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
								}
							}).create();
		}
		return null;
	}

	private void setDisplayElements() {

		arrMenuButtons[0] = (Button) findViewById(R.id.menu_user);
		arrMenuButtons[1] = (Button) findViewById(R.id.menu_customshot);
		arrMenuButtons[2] = (Button) findViewById(R.id.menu_customsettings);
		arrMenuButtons[3] = (Button) findViewById(R.id.menu_options);
		arrMenuButtons[4] = (Button) findViewById(R.id.menu_help);
		arrMenuButtons[5] = (Button) findViewById(R.id.menu_about);
		arrMenuButtons[6] = (Button) findViewById(R.id.menu_exit);

		arrMenuButtons[0].setText(SMBGuesstheNumber.strPlayerName);
		for (int i = 0; i < MENU_BUTTON_NUMBER; i++) {
			arrMenuButtons[i].setOnClickListener(this);
		}
	}

	private void preparePreference() {
		SMBGuesstheNumber.generalSettings = getSharedPreferences(
				CtrlMaintanence.PREF_NAME, 0);
		SMBGuesstheNumber.gameSettings = getSharedPreferences(
				CtrlMaintanence.GAME_SETTINGS_NAME, 0);
	}
}
